package keshe.dayin;


import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
//import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import java.io.*;
import javax.swing.JOptionPane;
import org.apache.poi.ss.usermodel.*;
//import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

public class Poi {

    public static void ExportExcel(List<student> list, String[] titles) {
        //titles = new String[]{"学号", "姓名", "专业", "数学", "语文", "英语"};
        HSSFWorkbook wb = new HSSFWorkbook();
        Sheet sheet = wb.createSheet();
        Row row = sheet.createRow(0);
        //给单元格设置样式
        CellStyle cellStyle = wb.createCellStyle();
        Font font = wb.createFont();
        //设置字体大小
        font.setFontHeightInPoints((short) 12);
        //设置字体加粗
        font.setBold(true);
        //给字体设置样式
        cellStyle.setFont(font);
        //设置单元格背景颜色
        cellStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
        //设置单元格填充样式(使用纯色背景颜色填充)
        cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        for (int i = 0; i < titles.length; i++) {
            Cell cell = row.createCell(i);
            cell.setCellValue(titles[i]);
            cell.setCellStyle(cellStyle);
            //设置列的宽度
            sheet.setColumnWidth(i, 200*50);
        }
        for (int j = 0; j < list.size(); j++) {
            Row rowData = sheet.createRow(j + 1);
            student student = list.get(j);
            Cell cell = rowData.createCell(0);
            cell.setCellValue(student.getNum());
            Cell cell2 = rowData.createCell(1);
            cell2.setCellValue(student.getName());
            Cell cell3 = rowData.createCell(2);
            cell3.setCellValue(student.getMajor());
            Cell cell4 = rowData.createCell(3);
            cell4.setCellValue(student.getMaths());
            Cell cell5 = rowData.createCell(4);
            cell5.setCellValue(student.getChinese());
            Cell cell6 = rowData.createCell(5);
            cell6.setCellValue(student.getEnglish());
        }
        String fileName = "D://学生成绩.xls";
        try {
            FileOutputStream fileOutputStream = new FileOutputStream(fileName);
            wb.write(fileOutputStream);
            wb.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }}
   /* public    Test (String[] titles)   {



            studentDao studentDao = new studentDao();
            List<student> list = studentDao.getstudentList();
        titles = new String[]{"学号", "姓名", "专业", "数学", "语文", "英语"};
            Poi.ExportExcel(list, titles);
          JOptionPane.showMessageDialog(null, "保存成功", "Massage",
        JOptionPane.INFORMATION_MESSAGE);


    }
}

*/